package com.ruicar.afs.cloud.apply.business.mq.channelfile;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import com.ruicar.afs.cloud.image.vo.ComAttachmentFileDataVo;
import com.ruicar.afs.cloud.parameter.commom.entity.TsysVehicleModel;
import com.ruicar.afs.cloud.parameter.commom.entity.VeBrand;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Ice
 * @date 2020/9/17 11:45
 */

@Slf4j
@Component
@AllArgsConstructor
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class ApplyChannelFile implements AfsMqBizProcessor<ComAttachmentFileDataVo> {
    private ComAttachmentFileService comAttachmentFileService;

    @Override
    public boolean processMessage(ComAttachmentFileDataVo entity) throws Exception {

        if (ObjectUtil.isNotNull(entity)) {
            System.out.println(entity.getComAttachmentFileList().get(0).getBusiNo());
            log.info("已成功接收到影像件MQ报文，解析报文中... ......(*￣1111111￣)ノ");
            this.getFileterParams(entity.getComAttachmentFileList());
        }
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.CHANNEL_CHANNEL_APPLY_POS_SEND_CHANNEL_FILE_APPLY;
    }

    @Override
    public boolean reQueueOnFail() {
        return false;
    }


    /**
     * 解析MQ报文
     *
     * @param entity
     */
    public void getFileterParams(List<ComAttachmentFile> entity) throws ParseException {

        if(ObjectUtil.isNotNull(entity)){
            // 先移除所有的影像资料信息
            comAttachmentFileService.remove(Wrappers.<ComAttachmentFile>query().lambda().eq(ComAttachmentFile::getBusiNo,entity.get(0).getBusiNo()).eq(ComAttachmentFile::getBelongNo,entity.get(0).getBelongNo()));
                for(ComAttachmentFile file:entity){
                    ComAttachmentFile fileTemp=new ComAttachmentFile();
                    BeanUtils.copyProperties(file, fileTemp);
                    fileTemp.setId(null);
                    comAttachmentFileService.save(fileTemp);
                }

        }

    }
}
